package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.lcb.manager.configuration.Config;
import com.lcb.manager.entity.CoalOutput;
import com.lcb.manager.entity.Electricity;
import com.lcb.manager.entity.ElectricityOutput;
import com.lcb.manager.repository.ElectricityOutputRepository;
import com.lcb.manager.repository.ElectricityRepository;
import com.lcb.manager.vo.ElectricityVo;
import com.lcb.manager.vo.Json;
import com.lcb.manager.vo.YearOutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/electricity")
public class ElectricityController {

    @Autowired
    private ElectricityRepository electricityRepository;

    @Autowired
    private ElectricityOutputRepository electricityOutputRepository;

    @RequestMapping(value = "/")
    public String index() {
        return "electricity";
    }

    @RequestMapping(value = "/add_template")
    public String addTemplate(ModelMap model) {
        List<Electricity> electricityList = electricityRepository.findAll();
        model.put("electricityList", electricityList);
        return "_add_electricity_output";
    }

    @ResponseBody
    @RequestMapping(value = "/get_name")
    public String getName() {
        Json result = new Json();
        List<Electricity> electricityList = electricityRepository.findAll();
        ElectricityVo vo = new ElectricityVo();
        vo.setName1(electricityList.get(0).getName());
        vo.setName2(electricityList.get(1).getName());
        vo.setName3(electricityList.get(2).getName());
        vo.setName4(electricityList.get(3).getName());
        vo.setName5(electricityList.get(4).getName());
        vo.setName6(electricityList.get(5).getName());
        vo.setName7(electricityList.get(6).getName());
        List<ElectricityVo> voList = new ArrayList<>();
        voList.add(vo);
        result.code = 0;
        result.data = voList;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit_name")
    public String editName(@RequestParam String field, @RequestParam String name) {
        int id = 1;
        switch (field) {
            case "name1":
                id = 1;
                break;
            case "name2":
                id = 2;
                break;
            case "name3":
                id = 3;
                break;
            case "name4":
                id = 4;
                break;
            case "name5":
                id = 5;
                break;
            case "name6":
                id = 6;
                break;
            case "name7":
                id = 7;
                break;
            default:
                break;
        }
        Electricity electricity = electricityRepository.findById(id).orElse(null);
        if (electricity != null) {
            electricity.setName(name);
            electricityRepository.save(electricity);
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/get_output")
    public String get_output(@RequestParam(defaultValue = "") String date, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<ElectricityOutput> pageResult;
        if (!date.isEmpty()) {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            pageResult = electricityOutputRepository.findByDateOrderByDateAsc(year, month, pageable);
        } else {
            pageResult = electricityOutputRepository.findByOrderByDateAsc(pageable);
        }
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public String add(@RequestParam Date date, @RequestParam String data) {
        Map<Integer, Integer> dataMap = JSON.parseObject(
                data, new TypeReference<Map<Integer, Integer>>() {
                });
        ElectricityOutput electricityOutput = new ElectricityOutput();
        electricityOutput.setDate(date);
        electricityOutput.setOutput1(dataMap.get(1));
        electricityOutput.setOutput2(dataMap.get(2));
        electricityOutput.setOutput3(dataMap.get(3));
        electricityOutput.setOutput4(dataMap.get(4));
        electricityOutput.setOutput5(dataMap.get(5));
        electricityOutput.setOutput6(dataMap.get(6));
        electricityOutput.setOutput7(dataMap.get(7));
        electricityOutputRepository.save(electricityOutput);
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/edit_output")
    public String editOutput(ElectricityOutput electricityOutput) {
        electricityOutputRepository.save(electricityOutput);
        return "ok";
    }

}
